package com.aaa.mrcai.service.impl;

import com.aaa.mrcai.mapper.GoodsCompanyMapper;
import com.aaa.mrcai.pojo.entity.GoodsCompany;
import com.aaa.mrcai.pojo.vo.GoodsCompanyVo;
import com.aaa.mrcai.query.GoodsCompanyQuery;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.service.GoodsCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Demo class
 *
 * @Author: Wang
 * @Date: Created in 20:25 2021/12/6
 */
@Service
public class GoodsCompanyServiceImpl implements GoodsCompanyService {

    @Autowired
    private GoodsCompanyMapper goodsCompanyMapper;


    @Autowired
    private HttpSession session;

    @Override
    public AjaxResult list(GoodsCompanyQuery query) {

        List<GoodsCompany> goodsCompanyList = goodsCompanyMapper.queryGoodsCompany(query);
        Long totalCount = goodsCompanyMapper.queryGoodsCompanyTotalCount(query);
        Map<String, Object> map = new HashMap<>();

        map.put("currentPageData", goodsCompanyList);
        map.put("currentDataTotal", totalCount);
        return AjaxResult.success(map);
    }



    @Override
    public AjaxResult addGoodsCompany(GoodsCompanyVo goodsCompanyVo) {
        long count = goodsCompanyMapper.isGoodsCompanyExists(goodsCompanyVo.getStype());
        if (count > 0) {
            return AjaxResult.defeated("对不起，商品规格已存在");
        }
        int i = goodsCompanyMapper.addGoodsCompany(goodsCompanyVo);

        if (i > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }

    @Override
    public AjaxResult getGoodsCompany(int id) {
        GoodsCompanyVo r = goodsCompanyMapper.getGoodsCompany(id);
        session.setAttribute("currentUpdateGoodsCompany",r);
        return AjaxResult.success(r);
    }

    @Override
    public AjaxResult updateGoodsCompany(GoodsCompanyVo goodsCompanyVo) {
        GoodsCompanyVo currentUpdateGoodsCompany = (GoodsCompanyVo) session.getAttribute("currentUpdateGoodsCompany");

       if (!goodsCompanyVo.getId().equals(currentUpdateGoodsCompany.getId())){
          return AjaxResult.defeated("不可以乱改哦");

        }
        long count = goodsCompanyMapper.isUpdateGoodsCompanyExists(goodsCompanyVo);

        if (count > 0) {
            return AjaxResult.defeated("已存在该商品规格");
        }

        int i = goodsCompanyMapper.updateGoodsCompany(goodsCompanyVo);

        if (i > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }

    @Override
    public AjaxResult deleteGoodsCompany(int id) {
        int  r = goodsCompanyMapper.deleteGoodsCompany(id);
        session.setAttribute("currentUpdateGoodsCompany",r);
        return AjaxResult.success(r);
    }


}
